package com.bbu.javaexam.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bbu.javaexam.domain.ExamManage;
import com.bbu.javaexam.domain.RespData;
import com.bbu.javaexam.service.impl.ExamManageServiceImpl;
import com.bbu.javaexam.util.RespDataHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author Alex
 * @version 1.0.0
 * @ClassName ExamManageController.java
 * @Description TODO
 */
@RestController
public class ExamManageController {
    @Autowired
    private ExamManageServiceImpl examManageService;

    @GetMapping("/exams")
    public RespData findAll() {
        return RespDataHandler.buildRespData(200, "请求成功", examManageService.findAll());
    }

    @GetMapping("/exams/{page}/{size}")
    public RespData findAll(@PathVariable("page") Integer page, @PathVariable Integer size) {
        RespData apiResult;
        Page<ExamManage> examManage = new Page<>(page, size);
        IPage<ExamManage> all = examManageService.findAll(examManage);
        apiResult = RespDataHandler.buildRespData(200, "请求成功！", all);
        return apiResult;
    }

    @GetMapping("/exam/{examCode}")
    public RespData findById(@PathVariable("examCode") Integer examCode) {
        ExamManage res = examManageService.findById(examCode);
        if (res == null) {
            return RespDataHandler.buildRespData(404, "考试编号不存在", null);
        }
        return RespDataHandler.buildRespData(200, "请求成功！", res);
    }

    @DeleteMapping("/exam/{examCode}")
    public RespData deleteById(@PathVariable("examCode") Integer examCode) {
        int res = examManageService.delete(examCode);
        return RespDataHandler.buildRespData(200, "删除成功", res);
    }

    @DeleteMapping("/examtimu/{examCode}/{type}")
    public RespData deletetimu(@PathVariable("examCode") Integer examCode, @PathVariable("type") String type) {
        int res = examManageService.deletetimu(examCode, type);
        return RespDataHandler.buildRespData(200, "删除成功", res);
    }

    @PutMapping("/exam")
    public RespData update(@RequestBody ExamManage exammanage) {
        int res = examManageService.update(exammanage);
        return RespDataHandler.buildRespData(200, "更新成功", res);
    }

    @PostMapping("/exam")
    public RespData add(@RequestBody ExamManage exammanage) {
        int res = examManageService.add(exammanage);
        if (res == 1) {
            return RespDataHandler.buildRespData(200, "添加成功", res);
        } else {
            return RespDataHandler.buildRespData(400, "添加失败", res);
        }
    }

    @GetMapping("/examManagePaperId")
    public RespData findOnlyPaperId() {
        ExamManage res = examManageService.findOnlyPaperId();
        if (res != null) {
            return RespDataHandler.buildRespData(200, "请求成功", res);
        }
        res = new ExamManage();
        res.setPaperId(0);
        return RespDataHandler.buildRespData(200, "请求成功", res);
    }
}
